package com.huawei.controller;

import com.huawei.common.BaseResult;
import com.huawei.common.dto.EmpDto;
import com.huawei.common.vo.PageInfoVo;
import com.huawei.domain.Emp;
import com.huawei.service.EmpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author LF
 * projectName:vue-cli
 * description:
 * time:2025-04-26 20:27
 */
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @PostMapping("/getEmpByPage")
    public BaseResult<PageInfoVo> getEmpByPage(@RequestBody PageInfoVo pageInfoVo) {
        PageInfoVo infoVo = empService.queryEmpByPage(pageInfoVo);
        return BaseResult.success(infoVo);
    }

    @PostMapping("/addEmp")
    public BaseResult addEmp(@RequestBody EmpDto empDto) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(empDto, emp);
        int i = empService.insertSelective(emp);
        if (i > 0) {
            return BaseResult.success();
        }
        return BaseResult.fail("添加失败");
    }

    @PostMapping("/updateEmp")
    public BaseResult updateEmp(@RequestBody EmpDto empDto) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(empDto, emp);
        int i = empService.updateByPrimaryKeySelective(emp);
        if (i > 0) {
            return BaseResult.success();
        }
        return BaseResult.fail("添加失败");
    }

    @PostMapping("/deleteEmpById")
    public BaseResult deleteEmpById(@RequestParam("empId") String id) {
        try {
            empService.deleteByPrimaryKey(Integer.valueOf(id));
            return BaseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.fail("删除失败");
        }
    }
}
